package com.sumainfor.zuul.conf;

import com.sumainfor.common.exception.LoginFailureException;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.util.R;
import com.sumainfor.common.util.RRException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class ErrorController {

    private Logger log = LoggerFactory.getLogger(getClass());

    @ExceptionHandler(RRException.class)
    public R rrException(Throwable t) {
        log.error("RRException 程序抛出自定义异常 ==>", t);
        return R.error(t.getMessage());
    }

    @ExceptionHandler(LoginFailureException.class)
    public JsonResult loginFailure(LoginFailureException e) {
        log.error("凭证错误: ", e);
        return JsonResult.error(e.getMessage(), e.getCode());
    }

    @ExceptionHandler(Throwable.class)
    public R exception(Throwable t) {
        log.error("服务器异常 500错误 ==>", t);
        return R.error(500, "服务器异常");
    }
}
